  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
           <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button> 
        </div>
        <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
        <script type="text/html" id="switc_status">
          <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="switch" {{(d.status=='1')?'checked' : '' }}>
        </script>
        <script type="text/html" id="table-content-list">
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
      </div>
    </div>
  </div>
<script>
layui.use(['admin', 'table','form','layedit'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,layedit = layui.layedit
  ,form = layui.form;

    //文章管理
  table.render({
    elem: '#LAY-app-content-list'
    ,url: '/api/invite/getList' //模拟接口
    , initSort: {
        field: 'id' 
        , type: 'desc'
    }
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'id', width: 50, title: '编号', align: 'center', width: 60}
      ,{field: 'ip_list', title: 'IP', edit:'text', templet: '#ip_list', width: 200}
      ,{field: 'name', title: '用户', edit:'text', width: 150}
      ,{field: 'articlenum', title: '黑名单添加原因', edit:'text'}
      ,{field: 'create_time', title: '留言时间', sort: true, width: 220}
      ,{field: 'status', title: '状态',templet: '#switc_status', width: 100, align: 'center'}
      ,{title: '操作', width: 220, align: 'center', fixed: 'right', toolbar: '#table-content-list'}
    ]]
    ,page: true
    ,limit: 20
    ,limits: [10, 20, 30, 50, 100]
    ,text: '对不起，加载出现异常！'
    ,parseData: function(res){ //res 即为原始返回的数据
      return {
        "code": res.code, //解析接口状态
        "msg": res.msg, //解析提示文本
        "count": res.data.total, //解析数据长度
        "data": res.data.data //解析数据列表
      };
    }
  });
  //单元格编辑
    table.on('edit(LAY-app-content-list)', function(obj){
        var data = obj.data;
        $.post("/api/invite/editShu", {
            action: 'user',
            id: data.id,
            category: obj.field,
            value: obj.value
        }, function (data) {
            layer.msg(data.msg ? data.msg : '保存失败！');
        });
    });
    //开关快捷管理
    form.on('switch(switch)', function (obj) {
        $.post("/api/invite/editShu", {
            action: 'editShu',
            id: this.value,
            status: this.name,
            value:  obj.elem.checked,
            category: 'status'
        }, function (data) {
            layer.msg(data.msg ? data.msg : '保存失败！');
        });
    }); 
  //监听工具条
  table.on('tool(LAY-app-content-list)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除所选数据？', function(index){
        obj.del();
        layer.close(index);

        admin.req({
          type: "POST",
          url: '/api/invite/del',
          data: { ids: data.id },
          dataType: "json",
          success: function (res) {
            layer.msg(res.msg);
          }
        });
        table.reload('LAY-app-content-list');

      });
    } else if(obj.event === 'edit'){
      var h = $(window).height() - 60;
      var w = $(window).width() - 10;
      admin.popup({
        title: '编辑'
        ,area: ['900px', '600px']
        ,id: 'LAY-popup-content-edit'
        ,success: function(layero, index){
          view(this.id).render('invite/add', data).done(function(){
            console.log(data);
            form.render(null, 'layuiadmin-app-form-list');
            // console.log(data);
            //监听提交
            form.on('submit(layuiadmin-app-form-submit)', function(data){
              var field = data.field; //获取提交的字段
              console.log(data.field);
              if(field.status=='on'){
                field.status = 1;
              }else{
                field.status = 0;
              }
              //提交 Ajax 成功后，关闭当前弹层并重载表格
              admin.req({
                type: "POST",
                url: '/api/invite/add',
                data: { data: field },
                dataType: "json",
                success: function (res) {
                  layer.msg(res.msg,{time:800});
                }
              });
              setTimeout(function() {
                layui.table.reload('LAY-app-content-list'); //重载表格
                layer.close(index); //执行关闭 
              }, 1000);
            });
          });
        }
      });
    }
  }); 
  form.render(null, 'app-content-list');
  var active = {
    batchdel: function(){
      var checkStatus = table.checkStatus('LAY-app-content-list')
      ,checkData = checkStatus.data; //得到选中的数据

      if(checkData.length === 0){
        return layer.msg('请选择数据');
      }
      var ids = [];   //声明数组
      for (var i = 0; i < checkData.length; i++) {
        ids.push(checkData[i].id);
      }
      layer.confirm('确定删除吗？', function(index) {
        // console.log(ids);
        admin.req({
          type: "POST",
          url: '/api/invite/del',
          data: { ids: ids },
          dataType: "json",
          success: function (res) {
            layer.msg(res.msg);
            table.reload('LAY-app-content-list');
          }
        });
      });
    }
    //添加
    ,add: function(othis){
      var h = $(window).height() - 60;
      var w = $(window).width() - 10;
      admin.popup({
        title: '添加留言'
        ,area: ['900px', '600px']
        ,id: 'LAY-popup-content-add'
        ,success: function(layero, index){
          view(this.id).render('invite/add').done(function(){
            form.render(null, 'layuiadmin-app-form-list');
            //监听提交
            form.on('submit(layuiadmin-app-form-submit)', function(data){
              var field = data.field; //获取提交的字段
                if(field.status=='on'){
                    field.status = 1;
                }else{
                    field.status = 0;
                }
                field.lang = lang;
                field.user_id = '1';

              //提交 Ajax 成功后，关闭当前弹层并重载表格
              admin.req({
                type: "POST",
                url: '/api/invite/add',
                data: { data: field },
                dataType: "json",
                success: function (res) {
                  layer.msg(res.msg,{time:800});
                }
              });

              layui.table.reload('LAY-app-content-list'); //重载表格
              layer.close(index); //执行关闭
            });
          });
        }
      });
    }
  };
  $('.layui-btn.layuiadmin-btn-list').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
</script>